package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.activity.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.activity.LoadingActivity;
import com.example.myapplication.activity.SongDetail1Activity;
import com.example.myapplication.entity.HotSongs;
import com.example.myapplication.entity.RecommendSongs;
import com.example.myapplication.tools.SetColor;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class RecyclerRecommendSongAdapter extends RecyclerView.Adapter<RecyclerRecommendSongAdapter.RecyclerRecommendSongHolder>{

    private Context context;
    private List<RecommendSongs.DataBean.DailySongsBean>recommendSongsList;
    private static int count=0;
    private boolean state =true;
    private BaseOnclickListener baseOnclickListener;

    public RecyclerRecommendSongAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<RecommendSongs.DataBean.DailySongsBean>artistList){
        this.recommendSongsList = artistList;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public RecyclerRecommendSongHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_recommend_song,parent,false);
        return new RecyclerRecommendSongHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerRecommendSongHolder holder, int position) {

        RoundedCorners roundedCorners = new RoundedCorners(60);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
            if (recommendSongsList != null) {
                RecommendSongs.DataBean.DailySongsBean dailySongs  = recommendSongsList.get(count%30);
                Glide.with(context).load(dailySongs.getAl().getPicUrl())
                        .centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .error(R.drawable.ic_baseline_refresh_24)
                        .placeholder(R.drawable.ic_baseline_refresh_24)
                        .apply(options)
                        .into(holder.img_song);
                holder.tv_song_name.setText(dailySongs.getName());
                holder.tv_song_desc.setText(dailySongs.getAr().get(0).getName()+"-"+dailySongs.getAl().getName());

                SetColor.asBitmap(dailySongs.getAl().getPicUrl(),context,holder.play_volume);
                holder.play_volume.setText(" ▶ "+dailySongs.getPop()+"万");
                count++;

                holder.view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Gson gson = new Gson();
                        String json = gson.toJson(dailySongs);
                        HotSongs hotSongs = gson.fromJson(json,HotSongs.class);
                        //

                        Intent intent1 = new Intent(context, SongDetail1Activity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("song",hotSongs);
                        intent1.putExtras(bundle);
                        context.startActivity(intent1);
                    }
                });
            }
    }

    @Override
    public int getItemCount() {
                   return 4;
    }


    class RecyclerRecommendSongHolder extends RecyclerView.ViewHolder{

        ImageView img_song;
        TextView tv_song_name;
        TextView tv_song_desc;
        TextView play_volume;
        View view;
        public RecyclerRecommendSongHolder(@NonNull View itemView) {
            super(itemView);
            img_song = itemView.findViewById(R.id.img_hall);
            tv_song_name = itemView.findViewById(R.id.tv_music_name);
            tv_song_desc = itemView.findViewById(R.id.tv_song_desc1);
            play_volume = itemView.findViewById(R.id.play_volume_hall);
            view = itemView.findViewById(R.id.view);

        }
    }

}
